package log;
import java.util.Calendar;


public class LogRecord {
	public Calendar time;
	public String content;
	
	public LogRecord(Calendar time, String content) {
		this.time = time;
		this.content = content;
	}
	
	public LogRecord(String str) {
		byte[] byteval = str.getBytes();
		for (int i=0; i<byteval.length; ++i)
			if (byteval[i] == ']') {
				time = analyzeHead(str.substring(0, i+1));
				content = str.substring(i+2);
				break;
			}
	}

	//[10/05/19 03:30:19]
	public static Calendar analyzeHead(String str) throws NumberFormatException {
		int y, m, d, h, min, s;
		int begin = 1;
		int end = findDigitEnd(str, begin);
		y = Integer.parseInt(str.substring(begin, end));
		begin = end + 1;
		end = findDigitEnd(str, begin);
		m = Integer.parseInt(str.substring(begin, end));
		begin = end + 1;
		end = findDigitEnd(str, begin);
		d = Integer.parseInt(str.substring(begin, end));
		begin = end + 1;
		end = findDigitEnd(str, begin);
		h = Integer.parseInt(str.substring(begin, end));
		begin = end + 1;
		end = findDigitEnd(str, begin);
		min = Integer.parseInt(str.substring(begin, end));
		begin = end + 1;
		end = findDigitEnd(str, begin);
		s = Integer.parseInt(str.substring(begin, end));

		Calendar c = Calendar.getInstance();
		c.set(y, m-1, d, h, min, s);
		return c;
	}
	
	private static int findDigitEnd(String str, int s) {
		byte[] byteval = str.getBytes();
		for (int i=s; i<byteval.length; ++i)
			if (byteval[i] > '9' || byteval[i] < '0')
				return i;
		return byteval.length;
	}
	
	public String toString() {
		String ret = "";
		ret += "[" + time.get(Calendar.YEAR);
		ret += "/" + (time.get(Calendar.MONTH)+1);
		ret += "/" + time.get(Calendar.DAY_OF_MONTH);
		ret += " " + time.get(Calendar.HOUR_OF_DAY);
		ret += ":" + time.get(Calendar.MINUTE);
		ret += ":" + time.get(Calendar.SECOND) + "]";
		ret += " " + content;
		return ret;
	}
}
